﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;
using System.Xml;
using DotNetNuke.Services.Installer;

namespace DotNetNuke.Tests.UI.WatiN.BuildVerification
{
    [TestFixture]
    [DependsOn(typeof(InstallTests))]
    [DependsOn(typeof(UpgradeTests))]
    [DependsOn(typeof(LoginTests))]
    [DependsOn(typeof(PageTests))]
    [DependsOn(typeof(ModuleTests))]
    public class ProfessionalFeatureTests : BVTBase
    {
        #region Test Setup and TearDown

        [SetUp]
        public void SetUp()
        {
            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }

        


        #endregion

        #region Tests

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Verify_Professional_Features_List(string version, string type)
        {
            if (VersionType.CompareTo("Community") == 0)
            {
                //This test does not apply to community packages, pass the test and ignore the results
                Assert.IsTrue(true);
            }
            else if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))  
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\ProfessionalFeatureTests\VerifyProFeaturesList\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                ProfessionalBase proPage = new ProfessionalBase(homePage);
                LoginPage loginPage = new LoginPage(homePage);
                loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                //Assert.IsFalse(homePage.PageContainsErrors());

                //Act 
                homePage.HostConsoleLink.Click();
                System.Threading.Thread.Sleep(1500);

                //Assert 
                watinAssert.AssertIsTrue(proPage.ManageWebServerConsoleDiv.Exists, "ManageWebServerDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.HealthMonitoringConsoleDiv.Exists, "HealthMonitoringDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ApplicationIntegrityConsoleDiv.Exists, "ApplicationIntegrityDivMissing.jpg");
                //watinAssert.AssertIsTrue(proPage.SpecialOffersConsoleDiv.Exists, "SpecialOffersDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.KnowledgeBaseConsoleDiv.Exists, "KnowledgeBaseDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.SoftwareAndDocumentationConsoleDiv.Exists, "SoftwareAndDocumentationDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.TechincalSupportConsoleDiv.Exists, "TechnicalSupportDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.MySupportTicketsConsoleDiv.Exists, "MySupportTicketsDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ActivateYourLicenseConsoleDiv.Exists, "ActivateLicenseDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.LicenseManagementConsoleDiv.Exists, "LicenseManagerDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.SearchCrawlerAdminConsoleDiv.Exists, "SearchCrawlerAdminDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.SecurityCenterConsoleDiv.Exists, "SecurityCenterDivMissing.jpg");
                watinAssert.AssertIsTrue(proPage.UserSwitcherConsoleDiv.Exists, "UserSwitcherDivMissing.jpg");
            }
        }

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Verify_Manage_Web_Servers(string version, string type)
        {
            if (VersionType.CompareTo("Community") == 0)
            {
                //This test does not apply to community packages, pass the test and ignore the results
                Assert.IsTrue(true);
            }
            else if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\ProfessionalFeatureTests\VerifyManageWebServers\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                ProfessionalBase proPage = new ProfessionalBase(homePage);
                LoginPage loginPage = new LoginPage(homePage);
                loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                //Assert.IsFalse(homePage.PageContainsErrors());

                //Act 
                homePage.HostConsoleLink.Click();
                System.Threading.Thread.Sleep(1500);
                proPage.ManageWebServerConsoleDiv.Link(Find.Any).Click();
                if (homePage.PageContainsErrors())
                {
                    //Take a screen Capture
                    ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnManageWebServersPage.jpg");
                    TestLog.WriteHighlighted("WARNING:");
                    TestLog.WriteLine("An error occurred on the Manage Web Servers Page, view the corresponding screen shot and exception text log for more information on the error. ");

                    //Record any recent exceptions in the log
                    ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                    HostSQLPage SQLPage = new HostSQLPage(homePage);
                    SQLPage.CheckForRecentExceptionsWriteToFile(CapturePath + "ManageWebServersExceptions.txt");

                    //Continue the test
                    homePage.GetMenuSpan("Host").FireEvent("onmouseover");
                    proPage.ProfessionalFeaturesMenuSpan.FireEvent("onmouseover");
                    proPage.ManageWebServersMenuSpan.Click();
                }

                //Assert 
                watinAssert.AssertIsTrue(homePage.ModuleTitleSpans.Count == 1, "tooManyModules.jpg");
                watinAssert.AssertIsTrue(proPage.ModuleTitleSpan.Text.Contains("Manage Web Servers"), "ModuleTitleMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Caching provider"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Web Farm Enabled?"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Use IIS App Name?"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Memory Usage"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Server:"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Memory Limit"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Private Bytes"), "ManageWebServersContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Cache Objects"), "ManageWebServersContentMissing.jpg");
            }
        }

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Verify_Application_Integrity(string version, string type)
        {
            if (VersionType.CompareTo("Community") == 0)
            {
                //This test does not apply to community packages, pass the test and ignore the results
                Assert.IsTrue(true);
            }
            else if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\ProfessionalFeatureTests\VerifyApplicationIntegrity\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                ProfessionalBase proPage = new ProfessionalBase(homePage);
                LoginPage loginPage = new LoginPage(homePage);
                loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                //Assert.IsFalse(homePage.PageContainsErrors());

                //Act 
                homePage.HostConsoleLink.Click();
                System.Threading.Thread.Sleep(1500);
                proPage.ApplicationIntegrityConsoleDiv.Link(Find.Any).Click();
                if (homePage.PageContainsErrors())
                {
                    //Take a screen Capture
                    ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnApplicationIntegrityPage.jpg");
                    TestLog.WriteHighlighted("WARNING:");
                    TestLog.WriteLine("An error occurred on the Application Integrity Page, view the corresponding screen shot and exception text log for more information on the error. ");

                    //Record any recent exceptions in the log
                    ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                    HostSQLPage SQLPage = new HostSQLPage(homePage);
                    SQLPage.CheckForRecentExceptionsWriteToFile(CapturePath + "ApplicationIntegrityExceptions.txt");

                    //Continue the test
                    homePage.GetMenuSpan("Host").FireEvent("onmouseover");
                    proPage.ProfessionalFeaturesMenuSpan.FireEvent("onmouseover");
                    proPage.ApplicationIntegrityMenuSpan.Click();
                }

                //Assert 
                watinAssert.AssertIsTrue(homePage.ModuleTitleSpans.Count == 1, "tooManyModules.jpg");
                watinAssert.AssertIsTrue(proPage.ModuleTitleSpan.Text.Contains("Application Integrity"), "FileIntegrityContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("File Integrity Summary"), "FileIntegrityContentMissing.jpg");
            }
        }

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Verify_Activate_Your_License(string version, string type)
        {
            if (VersionType.CompareTo("Community") == 0)
            {
                //This test does not apply to community packages, pass the test and ignore the results
                Assert.IsTrue(true);
            }
            else if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\ProfessionalFeatureTests\VerifyActivateYourLicense\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                ProfessionalBase proPage = new ProfessionalBase(homePage);
                LoginPage loginPage = new LoginPage(homePage);
                loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                //Assert.IsFalse(homePage.PageContainsErrors());

                //Act 
                homePage.HostConsoleLink.Click();
                System.Threading.Thread.Sleep(1500);
                proPage.ActivateYourLicenseConsoleDiv.Link(Find.Any).Click();
                if (homePage.PageContainsErrors())
                {
                    //Take a screen Capture
                    ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnActivateYourLicensePage.jpg");
                    TestLog.WriteHighlighted("WARNING:");
                    TestLog.WriteLine("An error occurred on the Activate Your License Page, view the corresponding screen shot and exception text log for more information on the error. ");

                    //Record any recent exceptions in the log
                    ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                    HostSQLPage SQLPage = new HostSQLPage(homePage);
                    SQLPage.CheckForRecentExceptionsWriteToFile(CapturePath + "ActivateLicenseExceptions.txt");

                    //Continue the test
                    homePage.GetMenuSpan("Host").FireEvent("onmouseover");
                    proPage.ProfessionalFeaturesMenuSpan.FireEvent("onmouseover");
                    proPage.ActivateLicenseMenuSpan.Click();
                }

                //Assert 
                watinAssert.AssertIsTrue(homePage.ModuleTitleSpans.Count == 1, "TooManyModules.jpg");
                watinAssert.AssertIsTrue(proPage.ModuleTitleSpan.Text.ToLower().Contains("activate your license"), "ActivateLicenseContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("No Licenses have been activated"), "ActivateLicenseContentMissing.jpg");
            }
        }


       // [Test]
       //// [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
       // [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
       // public void Verify_Telerik_Rad_Editor_is_Default_Text_Editor(string version, string type)
       // {
       //     if (VersionType.CompareTo("Community") == 0)
       //     {
       //         //This test does not apply to community packages, pass the test and ignore the results
       //         Assert.IsTrue(true);
       //     }
       //     else if (type.Equals("UpgradeOnly"))
       //     {
       //         TestLog.WriteLine("This test has not been run on an installation site.");
       //         Assert.IsTrue(true);
       //     }
       //     else if (type.Equals("InstallOnly"))
       //     {
       //         TestLog.WriteLine("This test has not been run on an upgraded site.");
       //         Assert.IsTrue(true);
       //     }
       //     else
       //     {
       //         //Arrange
       //         string capturePath = CapturePath + @"\ProfessionalFeatureTests\VerifyTelerikRadEditor\";
       //         WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
       //         if (!Directory.Exists(capturePath))
       //         {
       //             Directory.CreateDirectory(capturePath);
       //         }
       //         WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
       //         ProfessionalBase proPage = new ProfessionalBase(homePage);
       //         LoginPage loginPage = new LoginPage(homePage);
       //         PageSettingsPage newPage = new PageSettingsPage(homePage);
       //         string pageName = "Test Page";
       //         loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
       //         //Assert.IsFalse(homePage.PageContainsErrors());

       //         //Add A page and module.
       //         RibbonBar ribbonBar = new RibbonBar(homePage);
       //         ribbonBar.NewPageLink.Click();
       //         newPage.PageTemplateSelect.Select("None Specified");
       //         newPage.PageNameField.Value = pageName;
       //         newPage.UserPermissionField.Value = TestUsers.User1.UserName;
       //         newPage.AddUserPermissionLink.Click();
       //         newPage.UpdatePageLink.Click();
       //         System.Threading.Thread.Sleep(2000);

       //         ribbonBar.AddModuleToPage("", "HTML Pro");
       //         //Assert.IsFalse(homePage.PageContainsErrors());

       //         ModulePage modulePage = new ModulePage(homePage);
       //         //Write some text in the module
       //         System.Threading.Thread.Sleep(2000);
               
       //         //Act
       //         modulePage.GetEditModuleContentLink(0).Click();

       //         //Assert
       //         //Assert.IsFalse(homePage.PageContainsErrors());
       //         watinAssert.AssertIsTrue(proPage.TelerikEditorScriptElement.Exists, "TelerikRadEditorMissing.jpg");
       //     }
           
       // }

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Verify_Google_Analytics_Pro(string version, string type)
        {
            if (VersionType.CompareTo("Community") == 0)
            {
                //This test does not apply to community packages, pass the test and ignore the results
                Assert.IsTrue(true);
            }
            else if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\ProfessionalFeatureTests\VerifyGoogleAnalyticsPro\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                ProfessionalBase proPage = new ProfessionalBase(homePage);
                LoginPage loginPage = new LoginPage(homePage);
                loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                //Assert.IsFalse(homePage.PageContainsErrors());

                //Act 
                homePage.AdminConsoleLink.Click();
                System.Threading.Thread.Sleep(1500);
                proPage.GoogleAnalyticsProConsoleDiv.Link(Find.Any).Click();

                //Assert 
                //Assert.IsFalse(homePage.PageContainsErrors());
                watinAssert.AssertIsTrue(homePage.ModuleTitleSpans.Count == 1, "TooManyModules.jpg");
                watinAssert.AssertIsTrue(proPage.ModuleTitleSpan.Text.Contains("Google Analytics Pro"), "GoogleAnalyticsProContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Tracking ID"), "GoogleAnalyticsProContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Advanced Parameters"), "GoogleAnalyticsProContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Update"), "GoogleAnalyticsProContentMissing.jpg");
                watinAssert.AssertIsTrue(proPage.ContentPane.InnerHtml.Contains("Segmentation Rules"), "GoogleAnalyticsProContentMissing.jpg");
            }
        }

        #endregion
    }
}

